-- @owner:yangjian
-- @date:2024-4-1
-- @testpoint:1.去除大对象使用限制，测试lo_truncate64函数校验参数测试，合理报错

--清理环境
CREATE OR REPLACE FUNCTION f_opengauss_large_object_function_case0012_1()  
RETURNS VOID AS 
$$
BEGIN  
    BEGIN
    PERFORM lo_unlink(100);
    EXCEPTION
        WHEN others THEN
            NULL;
    end;
END;  
$$
LANGUAGE plpgsql;
/

select f_opengauss_large_object_function_case0012_1();

--环境准备
drop table if exists t_opengauss_large_object_function_case0012_1 cascade;
CREATE TABLE t_opengauss_large_object_function_case0012_1 (loid oid,fd int);
INSERT INTO t_opengauss_large_object_function_case0012_1 (loid) SELECT lo_create(100);

--step1:测试fd参数为存在值;expect:成功
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_truncate64(0,4);
end;
/

--step2:测试fd参数为不存在值;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_truncate64(1,4);
end;
/
end;
--step3:测试fd参数为非int类型;expect:失败
begin;
select lo_truncate64('a',4);
end;
/
end;
--step4:测试fd参数为负数;expect:失败
begin;
select lo_truncate64(-1,4);
end;
/
end;
--step5:测试fd参数为空值;expect:成功
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_truncate64(null,4);
end;
/

--step6:测试fd参数超过int4;expect:失败
begin;
select lo_truncate64(2147483648,4);
end;
/
end;
begin;
select lo_truncate64(-2147483649,4);
end;
/
end;
--step7:测试len参数为0;expect:成功
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_truncate64(0,0);
end;
/

--step8:测试len参数为负数;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_truncate64(0,-1);
end;
/
end;
--step9:测试len参数为null;expect:成功
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_truncate64(0,null);
end;
/

--step10:测试len参数超int8;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_truncate64(0,9223372036854775808);
end;
/
end;
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_truncate64(0,-9223372036854775809);
end;
/
end;
--step11:测试len参数为非int;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_truncate64(0,'a');
end;
/
end;
--step12:测试超过2g;expect:成功
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_truncate64(0,5000000000);
end;
/

--step13:测试len参数为int8边界;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_truncate64(0,9223372036854775807);
end;
/
end;
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_truncate64(0,-9223372036854775808);
end;
/
end;
--清理环境
select lo_unlink(loid) from t_opengauss_large_object_function_case0012_1;
select f_opengauss_large_object_function_case0012_1();
drop table if exists t_opengauss_large_object_function_case0012_1 cascade;
drop function f_opengauss_large_object_function_case0012_1();
